package br.ufpi.util.notification;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

/**
 * Classe respons�vel por enviar sms a partir do Kill Line.
 * Para tornar o envio mais efici�nte foi necess�rio a utiliza��o
 * de threads.
 * 
 * @author EasyTeam, created by Pedro Almir
 *
 */
public class SMSUtil {
	
	private final static String URL_DOMAIN = "www.dbmkt.com.br";
	private final static String URL_SUBDOMAIN = "/__envio/gw.php";
	private final static String KEY = "f7f7dbfbe951c7224abc996a3f276dfd";

	/**
	 * Send SMS
	 * @param sms
	 */
	public static void sendSMS(final SMS sms) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					/* Make Params */
					String params = String.format( "chave=%s&de=%s&tel=%s&msg=%s" , KEY , sms.getSourceName() , sms.getDestinationPhoneNumber() , sms.getMessage());
					/* Make URI */
					String uri = new URI("http", URL_DOMAIN, URL_SUBDOMAIN, params, null).toURL().toString();
					/* Just for debug */
//					System.out.println("\n\n" + uri + "\n\n");
					
					GetMethod http = new GetMethod(uri);
					HttpClient httpclient = new HttpClient();
					httpclient.executeMethod(http);
					
					String body = http.getResponseBodyAsString();
					System.out.println(body);
					
				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (URISyntaxException e) {
					e.printStackTrace();
				} catch (HttpException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

}
